<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="4" :md="12">
            <el-form-item label="供应商:" label-width="60px">
              <el-input
                clearable
                v-model="where.suppliercode"
                placeholder=""/>
            </el-form-item>
          </el-col>
          <el-col :md="4" :sm="12">
            <el-form-item label="企业地址:">
              <el-input
                clearable
                v-model="where.address"
                placeholder=""/>
            </el-form-item>
          </el-col>
          <el-col :md="4" :sm="12">
            <el-form-item label="专管员:" label-width="65px">
              <el-select
                clearable
                v-model="where.qcusername"
                placeholder="请选择专管员"
                class="ele-fluid">
                <el-option v-for="item in qcuserList" :label="item" :value="item" :key="item" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="3" :md="12">
            <el-form-item label="封存:" label-width="45px">
              <el-select
                clearable
                v-model="where.fcbz"
                placeholder="请选择封存状态"
                class="ele-fluid">
                <el-option label="N" value="N"/>
                <el-option label="Y" value="Y"/>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="4" :md="12">
            <el-form-item label="上传状态:">
              <el-select
                clearable
                v-model="where.status"
                placeholder="请选择状态"
                class="ele-fluid">
                <el-option label="未上传" value="0"/>
                <el-option label="已上传" value="1"/>
                <el-option label="未审批" value="8"/>
                <el-option label="已审批" value="9"/>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="5" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                size="small"
                @click="reload">查询
              </el-button>
              <el-button
                type="warning"
                icon="el-icon-search"
                class="ele-btn-icon"
                size="small"
                v-if="permission.includes('sys:supplier:sync')"
                @click="getCustomer">获取数据
              </el-button>
              <el-button @click="reset" size="small">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        :toolbar="false"
        :page-size="20"
        :header-cell-style="{'text-align':'center'}"
        @row-dblclick="showCustomerLogForm"
        height="calc(100vh - 210px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar" v-if="false">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:c37infckscbillnew:add')">添加
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="permission.includes('sys:c37infckscbillnew:dall')">删除
          </el-button>
        </template>
        <!-- 操作列 -->
        <template  slot="spzt" slot-scope="{row}">
          <a href="#" style="font-size: 12px" @click="openMaraLog(row)">{{row.spzt}}</a>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-upload2"
            @click="openEdit(row)"
            :disabled="!permission.includes('sys:supplier:submit')"
          >数据上传
          </el-link>
          <el-link
            type="success"
            slot="reference"
            :underline="false"
            icon="el-icon-document"
            @click="openLog(row)"
          >日志
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <LogList
      :dataItem="current"
      :visible.sync="showLog"
    />
    <MaraLogList
      :dataItem="current"
      :visible.sync="showMaraLog"
    />     <CustomerLog :visible="showCustomerLog" :data="selection"
                 @update:visible="updateShowCustomerLog"/>
    <!--    <BillDetail-->
    <!--      :dataItem="current"-->
    <!--      :visible.sync="showDetail"-->
    <!--    />-->

    <!-- 编辑弹窗 -->
    <!-- <c37infckscbillnew-edit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"/> -->
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import LogList from '../loglist.vue';
import CustomerLog from './supplier-log'
import MaraLogList from '../mara-log';
// import BillDetail from './billdetail.vue';
// import C37InfCkScBillNewEdit from './c37infckscbillnew-edit';

export default {
  name: 'KDLSupplier',
  // components: {C37InfCkScBillNewEdit},
  components: {LogList,CustomerLog,MaraLogList},
  computed: {
    ...mapGetters(["permission"]),
  },
  mounted() {
    this.getQcUserList();
  },
  data() {
    return {
      // 表格数据接口
      url: '/kdl/supplierindex',
      // 表格列配置
      columns: [
        {
          columnKey: 'index',
          type: 'index',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'suppliercode',
          label: '供应商编码',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'suppliername',
          label: '供应商名称',
          showOverflowTooltip: true,
          minWidth: 180,
          align: 'left'
        },
        {
          prop: 'address',
          label: '企业地址',
          showOverflowTooltip: true,
          minWidth: 250,
          align: 'left'
        },
        {
          prop: 'nation',
          label: '国家',
          showOverflowTooltip: true,
          minWidth: 60,
          align: 'center'
        },
        {
          prop: 'region',
          label: '地区',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center'
        },
        {
          prop: 'transzone',
          label: '运输区域',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center'
        },
        {
          prop: 'profession',
          label: '行业',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center'
        },
        {
          prop: 'restcode',
          label: '码上放心编码',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'left'
        },
        {
          prop: 'currency',
          label: '订单货币',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center'
        },
        {
          prop: 'fcbz',
          label: '封存',
          showOverflowTooltip: true,
          minWidth: 60,
          align: 'center'
        },
        {
          prop: 'status',
          label: '上传状态',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.statusFormat(row, column,cellValue);
          }
        },
        {
          prop: 'spzt',
          label: '审批状态',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
          slot:'spzt'
        },
        {
          prop: 'qcusername',
          label: '专管员',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'ncts',
          label: '更新时间',
          showOverflowTooltip: true,
          minWidth: 140,
          align: 'center'
        },
        // {
        //   prop: 'yewStaff',
        //   label: '业务员',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'tihWay',
        //   label: '提货方式',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'yewType',
        //   label: '业务类型',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'danjxcSide',
        //   label: '单据下传方',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'hanghao',
        //   label: '行号',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'shangpNo',
        //   label: '商品编号',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'jihNum',
        //   label: '开票数量',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'num',
        //   label: '实际数量',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'lot',
        //   label: '批次号',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'shengcChar',
        //   label: '生产日期',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'youxChar',
        //   label: '有效期',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'chonghFlg',
        //   label: '冲红标志',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'tuihReason',
        //   label: '退货原因',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'kub',
        //   label: '库别',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'khZzbm',
        //   label: 'NC组织编码',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'yezId',
        //   label: '九州通业主编码',
        //   showOverflowTooltip: true,
        //   minWidth: 140,
        //   align: 'center'
        // },
        {
          columnKey: 'action',
          label: '操作',
          width: 160,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {riqiBegin:this.getFromDate(),riqiEnd:new Date()},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      showLog: false,
      showMaraLog: false,
      showDetail:false,
      showCustomerLog:false,
    };
  },
  methods: {
    async getQcUserList(){
      var res = await this.$http.get('/kdl/qcuserlist/V',);
      if(res.data.code===0){
        this.qcuserList = res.data.data;
      }
    },
    updateShowCustomerLog(value)
    {
      this.showCustomerLog = value;
    },
    showCustomerLogForm(row){
      this.selection=[];
      console.log(row);
      this.selection.push(row);
      this.showCustomerLog =true;
    },

    statusFormat(row, column,cellValue) {
      const daterc = row[column.property]
      if(daterc !=null){
        return Number(cellValue)==1?"已上传":"未上传";
      }
    },
    getFromDate() {
      const timeOne = new Date()
      const year = timeOne.getFullYear()
      let month = timeOne.getMonth()
      let day = 1
      //month = month < 10 ? '0' + month : month
      //day = day < 10 ? '0' + day : day
      ///const NOW_MONTHS_AGO = `${year}-${month}-${day}`
      return new Date(year,month,day);
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    getCustomer(){
      const loading = this.$loading({lock: true});
      this.$http.post('/kdl/syncsupplier').then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 重置搜索 */
    reset() {
      this.where = {riqiBegin:this.getFromDate(),riqiEnd:new Date(),yewType:'销售出库'};
      this.reload();
    },
    openLog(row){
      console.log(row);
      this.current = row;
      this.current.chukNo = row.suppliercode;
      this.showLog = true;
    },
    openMaraLog(row){
      this.current = row;
      this.current.masterdatano = row.suppliercode;
      this.showMaraLog = true;
    },
    openDetail(row){
      this.current = row;
      this.showDetail = true;
    },

    /* 显示编辑 */
    openEdit(row) {
      this.$confirm('确定要将选中的供应商主数据上传至康德乐吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/kdl/supplier/'+ row.id).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.delete('/c37infckscbillnew/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的销售出库/购进退出上传单表吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.delete('/c37infckscbillnew/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
  }
}
</script>

<style scoped>
</style>
